/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities;

import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Random;
import rpg.Game;
import rpg.Location;
import rpg.Maps.Map;
import rpg.gfx.Bitmap;
import rpg.util.AStar;
import rpg.util.ImageUtil;

/**
 * rothens.tumblr.com
 * @author Rothens
 */
public class Zombie1 extends Enemy {

	private Bitmap bitmaps[];
	private int animState = 0;
	private AStar as;
	LinkedList<Location> path;
	public int nextCheck = 3;
	private int speed = 1;
	int animDelay = 5;
	private int dir = 0;
	private Location target;

	public Zombie1(Map map, Location loc) {
		super(150, loc);
		as = new AStar();
		this.map = map;
		target = loc;
		bitmaps = new Bitmap[12];
		BufferedImage image = ImageUtil.createImageIcon("/res/zombie1.png");
		for (int i = 0; i < 12; i++) {
			int col = i % 3;
			int row = i / 3;
			bitmaps[i] = new Bitmap(64, 64);
			image.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, bitmaps[i].width, bitmaps[i].height, bitmaps[i].pixels, 0, bitmaps[i].width);
		}
		Game.eh.addToActive(this);

	}

	public Zombie1(Map map, Location loc, Entity target) {
		super(150, loc);
		as = new AStar();
		this.map = map;
		this.target = loc;
		bitmaps = new Bitmap[12];
		BufferedImage image = ImageUtil.createImageIcon("/res/zombie1.png");
		for (int i = 0; i < 12; i++) {
			int col = i % 3;
			int row = i / 3;
			bitmaps[i] = new Bitmap(64, 64);
			image.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, bitmaps[i].width, bitmaps[i].height, bitmaps[i].pixels, 0, bitmaps[i].width);
		}
		Game.eh.addToActive(this);
	}

	@Override
	public boolean isPassable() {
		return false;
	}

	@Override
	public boolean tick() {
		//System.out.println("Zombie: " + loc.getX() + " " + loc.getY() + " oX:" + loc.getX()%64  + " oY:" + loc.getY()%64);
		if (targetEntity != null) {
			if (path == null || path.isEmpty() || nextCheck <= 0) {
				path = as.getPath(loc, targetEntity.getLocation(), map);
				nextCheck = 5;
			}

			target = path.getFirst();
			if (loc.equals(path.getFirst())) {
				path.removeFirst();
				nextCheck--;
			}
			speed = 1;
		} else {
			speed = 1;
		}
		animDelay--;
		if (animDelay <= 0) {
			animDelay = 5;
			animState++;
			animState %= 3;

		}
		int moveDir = loc.getDir(target);
		if (moveDir != -1) {
			dir = moveDir;
			if (!loc.move(moveDir, speed, map)) {
				target = loc.getPossibleLocation(map);
			}
		} else {
			Random rand = new Random();
			if (rand.nextBoolean()) {
				target = loc.getPossibleLocation(map);
			}
		}
		return true;
	}

	@Override
	public Bitmap getDrawGraphics() {
		return bitmaps[animState % 3 + dir * 3];
	}

	@Override
	void setTarget(Entity entity) {
		targetEntity = entity;
	}

	@Override
	public boolean damage(LivingEntity causer, int dmg) {
		boolean ret = super.damage(causer, dmg);
		setTarget(causer);
		return ret;
	}
}
